package sun.cn.common.cacheDto.proxy.handler;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.List;

import com.google.common.collect.Lists;
import sun.cn.common.cacheDto.proxy.ServiceProxy;

/**
 * 代理handler
 * @author sunlingao
 * @title
 * @date 2017年7月6日
 * @version
 */
public class ProxyHandler implements InvocationHandler{
	
	private Object obj;
	
	private List<ServiceProxy> serviceProxy;
	
	public ProxyHandler(Object obj,ServiceProxy proxy){
		this.obj = obj;
		this.serviceProxy = Lists.newArrayList(proxy);
	}
	
	public ProxyHandler(Object obj,List<ServiceProxy> proxys){
		this.obj = obj;
		this.serviceProxy = proxys;
	}

	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		Object result = null;
		for(ServiceProxy sp : serviceProxy){
			Object tmp = sp.proxy(args,method,obj);
			if(tmp != null)
				result = tmp;
		}
		return result;
	}

}
